﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.ObjectBuilder2;
using Microsoft.Practices.Unity;
using System.ComponentModel;
using Lilium.Reflection;

namespace Lilium.Unity.ContainerConfiguration
{
	public class ConfigurationReflectionPolicy : IContainerConfigurationPolicy
	{
        public ConfigurationReflectionPolicy(IUnityContainer container)
		{
            if (container == null) throw new ArgumentNullException("container");

            m_Container = container;
		}

		#region IInjectionConfigurationPolicy Members

		public void ConfigureInjection(IBuilderContext context)
		{
			if (context == null) throw new ArgumentNullException("context");

            var buildKey = context.BuildKey;

            foreach (var configurator in buildKey.Type.GetCustomAttributes<IContainerConfigurator>())
                configurator.ConfigureContainer(Container, buildKey);
		}

		#endregion

        #region private IUnityContainer Container

        private readonly IUnityContainer m_Container;

        private IUnityContainer Container
        {
            get
            {
                return m_Container;
            }
        }

        #endregion
	}
}